import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import BuyCar from '../views/buyCar.vue'
import Home1 from '../views/home/Home1'
import Home2 from '../views/home/Home2'

Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    component:Login
  },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [{
            path: '/h1',
            name: 'Home1',
            component: Home1,
        }, {
            path: '/h2',
            name: 'Home2',
            component: Home2,
        }]
    },
    {
        path: '/buycar',
        name: 'BuyCar',
        component: BuyCar
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    }
]

const router = new VueRouter({
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    console.log(to.path, from.path)
    let token = localStorage.getItem('token')
    if (token){
      next()
    }else {
      next('/login');
    }
})

export default router
